package cn.edu.nuc.onlinestore.network.server;

import java.io.IOException;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

import cn.edu.nuc.onlinestore.model.Goods;
import cn.edu.nuc.onlinestore.vo.AdminDo;
import cn.edu.nuc.onlinestore.vo.Cart;
import cn.edu.nuc.onlinestore.vo.Massage;
import cn.edu.nuc.onlinestore.vo.UserDo;
import cn.edu.nuc.onlinestore.vo.goodsDo;

public class ServerThread implements Runnable {

	//与单个客户端的链接socket
	private Socket client=null;
	private String mass;//返回用户操作信息
	private Boolean result;//返回操作的结果
	//存储当前所有链接用户列表
	private static Map<String,Socket> clients=new HashMap<>();
	private int people;
	public ServerThread(Socket client, int num) {
		// TODO Auto-generated constructor stub
		this.client=client;
		this.people=num;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		if(this.client==null||this.client.isClosed()){
			try {
				System.out.println(11);
				client.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
			Goods good=null;//用于查找操作返回结果
			Cart total=null;//用于封装总量的返回值
			int code = 0;
			Map<String, Goods> map=null;
			while(true){
			Massage msg=null;
					msg=ObjectStream.input(client);
			//进行解析msg操作
			while(msg!=null){
				//测试
				System.out.println(msg.getOpt());
				if(msg.getOpt().equals("zaixianyonghu")){
					code=people;
					result=true;
				}
				
					if((Object)msg.getClasss()instanceof UserDo){
						UserDo userdo=(UserDo) msg.getClasss();
						System.out.println("userdo"+msg.getOpt());
						switch(msg.getOpt()){
						case "check":result=userdo.userCheck();break;
						case "add":result=userdo.userAdd();break;
						case "update":result=userdo.userUpdatepassword(msg.getMsg());break;
					}
						mass=userdo.getMsg();
					}else if((Object)msg.getClasss()instanceof AdminDo){
						System.out.println("admindo"+msg.getOpt());
						AdminDo admindo=(AdminDo) msg.getClasss();
						switch(msg.getOpt()){
						case "check":{result=admindo.adminCheck();break;}
						case "add":result=admindo.adminAdd();break;
						case "update":result=admindo.adminUpdatepassword(msg.getMsg());break;
						}
						mass=admindo.getMsg();
					}else if((Object)msg.getClasss()instanceof goodsDo){
						System.out.println("goodsdo"+msg.getOpt());
						goodsDo goodsdo=(goodsDo) msg.getClasss();
						switch(msg.getOpt()){
						case "showlist":map=goodsdo.showGoodList();break;
						case "add":result=goodsdo.goodsAdd();break;
						case "remove":result=goodsdo.removegood();break;
						case "update":result=goodsdo.goodsUpdate(msg.getCode(),msg.getMsg());break;//利用Massage的code传递修改的数量，利用msg传递要修改的简介
						case "seachid":good=goodsdo.serachid(msg.getCode());break;
						case "seachname":good=goodsdo.serachname(msg.getMsg());break;
						}
						mass=goodsdo.getMsg();
					}else if((Object)msg.getClasss()instanceof Cart){
						System.out.println("cart"+msg.getOpt());
						Cart cart=(Cart) msg.getClasss();
						switch(msg.getOpt()){
						case "totalnum":code=cart.gettotalnum();break;
						case "showbuylist":map=cart.getGoodsshow();break;
						case "add":result=cart.add(msg.getMsg(),msg.getCode());break;
						case "totalprice":{
							cart.total();
							total=new Cart(cart.getTotalPrice(),cart.getTotalQuantity());
							break;
						}//利用Massage的code传递修改的数量，利用msg传递要修改的名称
						case "removeid":result=cart.remove(msg.getCode());
						case "removename":result=cart.remove(msg.getMsg());
						}
						mass=cart.getMsg();
					}
				//
				//
				//将上面返回的值通过Massage返回客户端
				String message=mass;
				String opt=result.toString();
				System.out.println("操作返回"+opt+message);
				while(map!=null){
					msg=new Massage(code,message,opt,map);
					break;
				}
				while(good!=null){
					msg=new Massage(code,message,opt,good);
					break;
				}
				while(total!=null){
					msg=new Massage(code,message,opt,total);
				}
				while(map==null&&good==null&&total==null){
					msg=new Massage(code,message,opt,good);
					System.out.println(msg.getMsg()+msg.getOpt());
					break;
				}
				ObjectStream.output(msg,client);
				msg=null;
				}
			}
		}
	
	
	public static Map<String, Socket> getClients() {
		// TODO Auto-generated method stub
		return clients;
	}

}
